package bb.lanxing.lib.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class PinnedHeaderRecyclerView extends RecyclerView {
    private PinnedSectionedHeaderAdapter mAdapter;
    private View mCurrentHeader;
    private int mCurrentHeaderViewType;
    private int mCurrentSection;
    private float mHeaderOffset;
    private int mHeightMode;
    private boolean mShouldPin;
    private int mWidthMode;
    private final RecyclerView.OnScrollListener scrollListener;
    private int sectionCount;

    public interface PinnedSectionedHeaderAdapter {
        int getItemCount();

        int getSectionCount();

        int getSectionForPosition(int i);

        View getSectionHeaderView(ViewGroup viewGroup, View view, int i, int i2);

        int getSectionHeaderViewType(int i);

        boolean isSectionHeaderPosition(int i);

        void registerAdapterDataObserver(RecyclerView.AdapterDataObserver adapterDataObserver);
    }

    public PinnedHeaderRecyclerView(Context context) {
        super(context);
        this.mCurrentHeaderViewType = 0;
        this.mShouldPin = true;
        this.mCurrentSection = 0;
        RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int i) {
                super.onScrollStateChanged(recyclerView, i);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int i, int i2) {
                super.onScrolled(recyclerView, i, i2);
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int findFirstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                int findLastVisibleItemPosition = (linearLayoutManager.findLastVisibleItemPosition() - linearLayoutManager.findFirstVisibleItemPosition()) + 1;
                View view = null;
                if (PinnedHeaderRecyclerView.this.mAdapter == null || PinnedHeaderRecyclerView.this.mAdapter.getItemCount() == 0 || !PinnedHeaderRecyclerView.this.mShouldPin || findFirstVisibleItemPosition < 0) {
                    PinnedHeaderRecyclerView.this.mCurrentHeader = null;
                    PinnedHeaderRecyclerView.this.mHeaderOffset = 0.0f;
                    for (int i3 = findFirstVisibleItemPosition; i3 < findFirstVisibleItemPosition + findLastVisibleItemPosition; i3++) {
                        View childAt = PinnedHeaderRecyclerView.this.getChildAt(i3);
                        if (childAt != null) {
                            childAt.setVisibility(View.VISIBLE);
                        }
                    }
                    return;
                }
                int sectionForPosition = PinnedHeaderRecyclerView.this.mAdapter.getSectionForPosition(findFirstVisibleItemPosition);
                int sectionHeaderViewType = PinnedHeaderRecyclerView.this.mAdapter.getSectionHeaderViewType(sectionForPosition);
                PinnedHeaderRecyclerView pinnedHeaderRecyclerView = PinnedHeaderRecyclerView.this;
                if (pinnedHeaderRecyclerView.mCurrentHeaderViewType == sectionHeaderViewType) {
                    view = PinnedHeaderRecyclerView.this.mCurrentHeader;
                }
                pinnedHeaderRecyclerView.mCurrentHeader = pinnedHeaderRecyclerView.getSectionHeaderView(sectionForPosition, sectionHeaderViewType, view);
                PinnedHeaderRecyclerView.this.mCurrentHeaderViewType = sectionHeaderViewType;
                PinnedHeaderRecyclerView.this.mHeaderOffset = 0.0f;
                for (int i4 = findFirstVisibleItemPosition; i4 < findFirstVisibleItemPosition + findLastVisibleItemPosition; i4++) {
                    if (PinnedHeaderRecyclerView.this.mAdapter.isSectionHeaderPosition(i4)) {
                        View childAt2 = PinnedHeaderRecyclerView.this.getChildAt(i4 - findFirstVisibleItemPosition);
                        float top = childAt2.getTop();
                        childAt2.setVisibility(View.VISIBLE);
                        if (PinnedHeaderRecyclerView.this.mCurrentHeader.getMeasuredHeight() >= top && top > 0.0f) {
                            PinnedHeaderRecyclerView.this.mHeaderOffset = top - childAt2.getHeight();
                        } else if (top <= 0.0f) {
                            childAt2.setVisibility(View.INVISIBLE);
                        }
                    }
                }
                PinnedHeaderRecyclerView.this.postInvalidate();
            }
        };
        this.scrollListener = onScrollListener;
        addOnScrollListener(onScrollListener);
    }

    public PinnedHeaderRecyclerView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mCurrentHeaderViewType = 0;
        this.mShouldPin = true;
        this.mCurrentSection = 0;
        RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int i) {
                super.onScrollStateChanged(recyclerView, i);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int i, int i2) {
                super.onScrolled(recyclerView, i, i2);
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int findFirstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                int findLastVisibleItemPosition = (linearLayoutManager.findLastVisibleItemPosition() - linearLayoutManager.findFirstVisibleItemPosition()) + 1;
                View view = null;
                if (PinnedHeaderRecyclerView.this.mAdapter == null || PinnedHeaderRecyclerView.this.mAdapter.getItemCount() == 0 || !PinnedHeaderRecyclerView.this.mShouldPin || findFirstVisibleItemPosition < 0) {
                    PinnedHeaderRecyclerView.this.mCurrentHeader = null;
                    PinnedHeaderRecyclerView.this.mHeaderOffset = 0.0f;
                    for (int i3 = findFirstVisibleItemPosition; i3 < findFirstVisibleItemPosition + findLastVisibleItemPosition; i3++) {
                        View childAt = PinnedHeaderRecyclerView.this.getChildAt(i3);
                        if (childAt != null) {
                            childAt.setVisibility(View.VISIBLE);
                        }
                    }
                    return;
                }
                int sectionForPosition = PinnedHeaderRecyclerView.this.mAdapter.getSectionForPosition(findFirstVisibleItemPosition);
                int sectionHeaderViewType = PinnedHeaderRecyclerView.this.mAdapter.getSectionHeaderViewType(sectionForPosition);
                PinnedHeaderRecyclerView pinnedHeaderRecyclerView = PinnedHeaderRecyclerView.this;
                if (pinnedHeaderRecyclerView.mCurrentHeaderViewType == sectionHeaderViewType) {
                    view = PinnedHeaderRecyclerView.this.mCurrentHeader;
                }
                pinnedHeaderRecyclerView.mCurrentHeader = pinnedHeaderRecyclerView.getSectionHeaderView(sectionForPosition, sectionHeaderViewType, view);
                PinnedHeaderRecyclerView.this.mCurrentHeaderViewType = sectionHeaderViewType;
                PinnedHeaderRecyclerView.this.mHeaderOffset = 0.0f;
                for (int i4 = findFirstVisibleItemPosition; i4 < findFirstVisibleItemPosition + findLastVisibleItemPosition; i4++) {
                    if (PinnedHeaderRecyclerView.this.mAdapter.isSectionHeaderPosition(i4)) {
                        View childAt2 = PinnedHeaderRecyclerView.this.getChildAt(i4 - findFirstVisibleItemPosition);
                        float top = childAt2.getTop();
                        childAt2.setVisibility(View.VISIBLE);
                        if (PinnedHeaderRecyclerView.this.mCurrentHeader.getMeasuredHeight() >= top && top > 0.0f) {
                            PinnedHeaderRecyclerView.this.mHeaderOffset = top - childAt2.getHeight();
                        } else if (top <= 0.0f) {
                            childAt2.setVisibility(View.INVISIBLE);
                        }
                    }
                }
                PinnedHeaderRecyclerView.this.postInvalidate();
            }
        };
        this.scrollListener = onScrollListener;
        addOnScrollListener(onScrollListener);
    }

    public PinnedHeaderRecyclerView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mCurrentHeaderViewType = 0;
        this.mShouldPin = true;
        this.mCurrentSection = 0;
        RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int i2) {
                super.onScrollStateChanged(recyclerView, i2);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int i2, int i22) {
                super.onScrolled(recyclerView, i2, i22);
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int findFirstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                int findLastVisibleItemPosition = (linearLayoutManager.findLastVisibleItemPosition() - linearLayoutManager.findFirstVisibleItemPosition()) + 1;
                View view = null;
                if (PinnedHeaderRecyclerView.this.mAdapter == null || PinnedHeaderRecyclerView.this.mAdapter.getItemCount() == 0 || !PinnedHeaderRecyclerView.this.mShouldPin || findFirstVisibleItemPosition < 0) {
                    PinnedHeaderRecyclerView.this.mCurrentHeader = null;
                    PinnedHeaderRecyclerView.this.mHeaderOffset = 0.0f;
                    for (int i3 = findFirstVisibleItemPosition; i3 < findFirstVisibleItemPosition + findLastVisibleItemPosition; i3++) {
                        View childAt = PinnedHeaderRecyclerView.this.getChildAt(i3);
                        if (childAt != null) {
                            childAt.setVisibility(View.VISIBLE);
                        }
                    }
                    return;
                }
                int sectionForPosition = PinnedHeaderRecyclerView.this.mAdapter.getSectionForPosition(findFirstVisibleItemPosition);
                int sectionHeaderViewType = PinnedHeaderRecyclerView.this.mAdapter.getSectionHeaderViewType(sectionForPosition);
                PinnedHeaderRecyclerView pinnedHeaderRecyclerView = PinnedHeaderRecyclerView.this;
                if (pinnedHeaderRecyclerView.mCurrentHeaderViewType == sectionHeaderViewType) {
                    view = PinnedHeaderRecyclerView.this.mCurrentHeader;
                }
                pinnedHeaderRecyclerView.mCurrentHeader = pinnedHeaderRecyclerView.getSectionHeaderView(sectionForPosition, sectionHeaderViewType, view);
                PinnedHeaderRecyclerView.this.mCurrentHeaderViewType = sectionHeaderViewType;
                PinnedHeaderRecyclerView.this.mHeaderOffset = 0.0f;
                for (int i4 = findFirstVisibleItemPosition; i4 < findFirstVisibleItemPosition + findLastVisibleItemPosition; i4++) {
                    if (PinnedHeaderRecyclerView.this.mAdapter.isSectionHeaderPosition(i4)) {
                        View childAt2 = PinnedHeaderRecyclerView.this.getChildAt(i4 - findFirstVisibleItemPosition);
                        float top = childAt2.getTop();
                        childAt2.setVisibility(View.VISIBLE);
                        if (PinnedHeaderRecyclerView.this.mCurrentHeader.getMeasuredHeight() >= top && top > 0.0f) {
                            PinnedHeaderRecyclerView.this.mHeaderOffset = top - childAt2.getHeight();
                        } else if (top <= 0.0f) {
                            childAt2.setVisibility(View.INVISIBLE);
                        }
                    }
                }
                PinnedHeaderRecyclerView.this.postInvalidate();
            }
        };
        this.scrollListener = onScrollListener;
        addOnScrollListener(onScrollListener);
    }

    @Override
    public void setAdapter(RecyclerView.Adapter adapter) {
        super.setAdapter(adapter);
        PinnedSectionedHeaderAdapter pinnedSectionedHeaderAdapter = (PinnedSectionedHeaderAdapter) adapter;
        this.mAdapter = pinnedSectionedHeaderAdapter;
        this.mCurrentHeader = null;
        this.sectionCount = pinnedSectionedHeaderAdapter.getSectionCount();
        this.mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                int findFirstVisibleItemPosition;
                int sectionForPosition;
                if (PinnedHeaderRecyclerView.this.mAdapter.getItemCount() <= 0 || !PinnedHeaderRecyclerView.this.mShouldPin || (findFirstVisibleItemPosition = ((LinearLayoutManager) PinnedHeaderRecyclerView.this.getLayoutManager()).findFirstVisibleItemPosition()) == -1 || (sectionForPosition = PinnedHeaderRecyclerView.this.mAdapter.getSectionForPosition(findFirstVisibleItemPosition)) < 0) {
                    return;
                }
                int sectionHeaderViewType = PinnedHeaderRecyclerView.this.mAdapter.getSectionHeaderViewType(sectionForPosition);
                PinnedHeaderRecyclerView pinnedHeaderRecyclerView = PinnedHeaderRecyclerView.this;
                pinnedHeaderRecyclerView.mCurrentHeader = pinnedHeaderRecyclerView.getSectionHeaderView(sectionForPosition, sectionHeaderViewType, pinnedHeaderRecyclerView.mCurrentHeaderViewType != sectionHeaderViewType ? null : PinnedHeaderRecyclerView.this.mCurrentHeader);
                PinnedHeaderRecyclerView.this.mCurrentHeaderViewType = sectionHeaderViewType;
                PinnedHeaderRecyclerView.this.invalidate();
            }

            @Override
            public void onItemRangeChanged(int i, int i2) {
                onChanged();
            }

            @Override
            public void onItemRangeInserted(int i, int i2) {
                onChanged();
            }

            @Override
            public void onItemRangeMoved(int i, int i2, int i3) {
                onChanged();
            }

            @Override
            public void onItemRangeRemoved(int i, int i2) {
                onChanged();
            }
        });
    }

    public void setPinnable(boolean z) {
        this.mShouldPin = z;
    }

    public View getSectionHeaderView(int i, int i2, View view) {
        int sectionCount = this.mAdapter.getSectionCount();
        boolean z = i != this.mCurrentSection || view == null || sectionCount != this.sectionCount;
        this.sectionCount = sectionCount;
        View sectionHeaderView = this.mAdapter.getSectionHeaderView(this, view, i, i2);
        if (z) {
            ensurePinnedHeaderLayout(sectionHeaderView);
            this.mCurrentSection = i;
        }
        return sectionHeaderView;
    }

    private void ensurePinnedHeaderLayout(View view) {
        int makeMeasureSpec;
        if (view.isLayoutRequested()) {
            int makeMeasureSpec2 = View.MeasureSpec.makeMeasureSpec(getMeasuredWidth(), this.mWidthMode);
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams != null && layoutParams.height > 0) {
                makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
            } else {
                makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            }
            view.measure(makeMeasureSpec2, makeMeasureSpec);
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (this.mAdapter == null || !this.mShouldPin || this.mCurrentHeader == null) {
            return;
        }
        int save = canvas.save();
        canvas.translate(0.0f, this.mHeaderOffset);
        canvas.clipRect(0, 0, getWidth(), this.mCurrentHeader.getMeasuredHeight());
        this.mCurrentHeader.draw(canvas);
        canvas.restoreToCount(save);
    }

    @Override
    public void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        this.mWidthMode = View.MeasureSpec.getMode(i);
        this.mHeightMode = View.MeasureSpec.getMode(i2);
    }
}
